package com.zabra.zl.ui.profit.presenter;

import com.zabra.zl.App;
import com.zabra.zl.base.BasePresenter;
import com.zabra.zl.data.ProfitBean;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.profit.contract.ProfitContract;
import com.zabra.zl.ui.profit.model.ProfitModel;
import com.zabra.zl.utils.SharedPreferencesUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProfitPresenter extends BasePresenter<ProfitContract.View> implements ProfitContract.Presenter {
    private ProfitContract.Model model;

    public ProfitPresenter() {
        model = new ProfitModel();
    }

    @Override
    public void getData(int orderid) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getData(orderid).enqueue(new Callback<ProfitBean>() {
            @Override
            public void onResponse(Call<ProfitBean> call, Response<ProfitBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<ProfitBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getSubmitData(int orderid, String money) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getSubmitData(orderid, money).enqueue(new Callback<SimpleBean>() {
            @Override
            public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<SimpleBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }
}
